package com.zimmur.app.mocolink.background.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zimmur.app.mocolink.background.common.PropertiesCommon;
import com.zimmur.app.mocolink.background.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.background.common.StatusCodeEnum;
import com.zimmur.app.mocolink.domain.form.QueryGoodsForm;
import com.zimmur.app.mocolink.domain.param.GoodsParam;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.GoodsInfo;
import com.zimmur.app.mocolink.domain.vo.GoodsVo;
import com.zimmur.app.mocolink.domain.vo.UserCommentInfo;
import com.zimmur.app.mocolink.service.IGoodsService;
import com.zimmur.app.mocolink.service.IUserCommentService;

@RestController
@RequestMapping("/restful/goods")
public class GoodsController extends BaseController{
	@Autowired
	private IGoodsService goodsService;
	@Autowired
	private IUserCommentService userCommentService;
	
	@RequestMapping("/getGoodsDetail")
	public ReturnJsonEntity getGoodsDetail(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		Integer goodsId = getParam("goodsId", 0);
		GoodsVo goodsVo = goodsService.selectGoodsDetail(goodsId, userId);
		if(goodsVo!=null){
			changePrice(goodsVo.getGoodsInfo(),null);
			PageUtil<UserCommentInfo> page = userCommentService.selectByGoodsId(goodsId, 1);
			goodsVo.setCommentList(page);
			entity.setData(goodsVo);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/queryComment")
	public ReturnJsonEntity queryComment(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer goodsId = getParam("goodsId", 0);
		Integer pageNo = getParam("pageNo", 1);
		PageUtil<UserCommentInfo> page = userCommentService.selectByGoodsId(goodsId, pageNo);
		if(page!=null){
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/queryGoods")
	public ReturnJsonEntity queryGoods(QueryGoodsForm form){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		GoodsParam goodsParam = getGoodsParam(form);
		PageUtil<GoodsInfo> page = goodsService.queryGoodsByParam(goodsParam);
		if(page!=null){
			changePrice(null,page.getItemList());
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/queryFeaturedGoods")
	public ReturnJsonEntity queryFeaturedGoods(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer pageNo = getParam("pageNo", 1);
		Integer userId =getTokenUserId();
		GoodsParam goodsParam = new GoodsParam();
		goodsParam.setIsSp(1);//推荐商品
		goodsParam.setPageNo(pageNo);
		goodsParam.setUserId(userId);
		goodsParam.setOrderByClause(" a.pubdate desc,a.id desc");
		PageUtil<GoodsInfo> page = goodsService.queryGoodsByParam(goodsParam);
		if(page!=null){
			changePrice(null,page.getItemList());
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/newGoods")
	public ReturnJsonEntity newGoods(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer pageNo = getParam("pageNo", 1);
		GoodsParam goodsParam =new GoodsParam();
		goodsParam.setPageNo(pageNo);
		String orderByClause = "a.pubdate desc,a.id desc";
		goodsParam.setOrderByClause(orderByClause);
		PageUtil<GoodsInfo> page = goodsService.queryGoodsByParam(goodsParam);
		if(page!=null){
			changePrice(null,page.getItemList());
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/HotGoods")
	public ReturnJsonEntity HotGoods(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer pageNo = getParam("pageNo", 1);
		GoodsParam goodsParam =new GoodsParam();
		goodsParam.setPageNo(pageNo);
		String orderByClause = "a.brower_num desc,a.id desc";
		goodsParam.setOrderByClause(orderByClause);
		PageUtil<GoodsInfo> page = goodsService.queryGoodsByParam(goodsParam);
		if(page!=null){
			changePrice(null,page.getItemList());
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/comprehensiveGoods")
	public ReturnJsonEntity comprehensiveGoods(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer pageNo = getParam("pageNo", 1);
		GoodsParam goodsParam =new GoodsParam();
		goodsParam.setPageNo(pageNo);
		String orderByClause = "a.sale_num desc,a.id desc";
		goodsParam.setOrderByClause(orderByClause);
		PageUtil<GoodsInfo> page = goodsService.queryGoodsByParam(goodsParam);
		if(page!=null){
			changePrice(null,page.getItemList());
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	private GoodsParam getGoodsParam(QueryGoodsForm form){
		GoodsParam param = new GoodsParam();
		Integer pageNo = form.getPageNo();
		if(pageNo!=null && pageNo>0){
			param.setPageNo(pageNo);
		}else{
			param.setPageNo(1);
		}
		Integer typeId =form.getGoodsType();
		if(typeId != null && typeId>0){
			param.setTypeId(typeId);
		}
		Integer minPrice=form.getMinPrice();
		if(minPrice!=null && minPrice>=0){
			param.setMinPrice(minPrice);
		}
		Integer maxPrice=form.getMaxPrice();
		if(maxPrice!=null && maxPrice>0){
			param.setMaxPrice(maxPrice);
		}
		Integer minimumQuantity = form.getMinimumQuantity();
		if(minimumQuantity!=null && minimumQuantity>=0){
			param.setMinimumQuantity(minimumQuantity);
		}
		Integer quarter = form.getQuarter();
		if(quarter!=null && quarter>0 && quarter<5){
			param.setQuarter(quarter);
		}
		String keyword = form.getKeyword();
		if(!StringUtils.isBlank(keyword)){
			param.setKeyword(keyword);
		}
		String orderByClause="";
		Integer price = form.getPrice();
		if(price!=null && price ==1){
			orderByClause+=" a.price desc,";
		}else if(price!=null && price == -1){
			orderByClause+=" a.price asc,";
		}
		Integer sidx = form.getSidx();
		if(sidx!=null && sidx == 2){
			orderByClause+="a.pubdate desc,a.id desc";
		}else{
			orderByClause+="a.sale_num desc,a.brower_num desc,a.id desc";
		}
		param.setOrderByClause(orderByClause);
		return param;
	}
	
	private void changePrice(GoodsInfo info,List<GoodsInfo> infoList){
		String pubUserId = PropertiesCommon.getProperty("public.userId");
		int pUserId = Integer.valueOf(pubUserId);
		Integer userId = getTokenUserId();
		if(userId==null || userId==pUserId){
			if(info!=null){
				info.setPrice("***");
			}
			if(infoList!=null && infoList.size()>0){
				for(GoodsInfo goodsInfo:infoList){
					goodsInfo.setPrice("***");
				}
			}
		}
	}
}
